package com.flca.frw.components
{
	import com.flca.frw.interfaces.ICrudCtlr;
	import flash.display.DisplayObject;
	import mx.containers.VBox;
	import mx.containers.Panel;
	import mx.controls.Alert;
	import com.flca.frw.interfaces.ISimpleCtlr;
	import com.flca.frw.components.SimpleBaseCtrl;
	
	public class DetailCtrl extends Panel implements ICrudCtlr
	{
		[Inspectable]
		public var onSaveClicked : Function;
		
		[Inspectable]
		public var onCancelClicked : Function;

		[Inspectable]
		public var detailView : DisplayObject;

		protected var modelGetter : Function;

		public function setModelGetter(value:Function) : void {
			modelGetter = value;			
		}

		/*
		This method should called after al the properties from above are 
		set and before an updateViewFromModel
		*/
		public function init(aInitialHash:int = 0):void {
			
			view.idDetailView.removeAllChildren();
			if (detailView != null) {
				(detailView as ISimpleCtlr).setModelGetter(modelGetter);
				
				view.idDetailView.addChild(detailView);
				if (detailView is ISimpleCtlr) {
					(detailView as ISimpleCtlr).init();
				}
			} else {
				Alert.show("detail view not assigned");
			}
		}
		
		public function getCurrentMdl():Object {
			return modelGetter();
		}
		
		public function close():void {
			
		}
		
		public function validate() : Boolean {
			if (detailView != null && detailView is ISimpleCtlr) {
				return (detailView as ISimpleCtlr).validate();
			} else {
				return true;
			}	
		}
		
		public function getDisplayName():String {
			return "";
		}
		
		public function getInitialHash():int {
			return 0;
		}
		
		public function updateModelFromView():void {
			if (detailView != null) {
				(detailView as ISimpleCtlr).updateModelFromView();
			}			
		}
		
		public function updateViewFromModel():void {
			if (detailView != null) {
				(detailView as ISimpleCtlr).updateViewFromModel();
			}			
		}
		
		public function getCallbackReadyForClose():Function {
			return null;
		}

		public function getCallbackSaveComplete():Function {
			return null;
		}
		
		private function get view() : DetailForm {
			return this as DetailForm;
		}
		
        protected function cancelClicked():void {
			if (onCancelClicked != null) {
				onCancelClicked();
			} else {
				Alert.show("onCancelClicked not assigned!");
			}
        }
        
        protected function saveClicked():void {
			if (onSaveClicked != null) {
				onSaveClicked();
			} else {
				Alert.show("onSaveClicked not assigned!");
			}
        }
		
	}
}